/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rnavigator.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.JPanel;
import rnavigator.model.Base;
import rnavigator.model.Coord2D;
import rnavigator.model.Knot;
import rnavigator.model.Model;
import rnavigator.model.Structure;

/**
 * @author Elias la Cour
 */
public class StructurePanel extends JPanel {

    private boolean debug = false;
    private Structure structure = null;
    private int xCenterOfPanel, yCenterOfPanel;
    Coord2D centerOfPanel;
    double radius;
    boolean drawn = false;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        xCenterOfPanel = getWidth() / 2;
        yCenterOfPanel = getHeight() / 2;
        radius = Math.min(getWidth() / 3, getHeight() / 3);
        //centerOfPanel = new Coord2D(xCenterOfPanel, yCenterOfPanel);
        if (Model.structure != null) {
            setStructure(Model.structure);
            g2d.drawString("C", xCenterOfPanel, yCenterOfPanel);
            System.out.println("Structure loaded in paintComponent()...");
            setFeynmanTransformer(g2d);
            drawCircularFeynmanFromUnitMatrix(g2d);
        }
    }

    /**
     * @param structure the structure to set
     */
    public void setStructure(Structure structure) {
        System.out.println("new structure set in Structurepanel()");
        this.structure = structure;
        repaint();
    }

    public void setFeynmanTransformer(Graphics2D g2d) {
        AffineTransform transformer = new AffineTransform();
        transformer.setToTranslation(xCenterOfPanel, yCenterOfPanel);
        g2d.setTransform(transformer);
    }

    public void drawCircularFeynmanFromUnitMatrix(Graphics2D g2d) {
        Base b1;
        Arc2D arc;
        LinkedList<Knot> knots;
        ArrayList<Base> bases;
        double ucX, ucY, ucRadius, ucAngSt, ucAngExt;
        int ucClosure;

        if (debug) {
            System.out.println("drawCircularFeynmanFromUnitMatrix called...");
        }
        bases = structure.getBases();
        knots = structure.getKnots();

        //loop to draw bases
        for (Base b : bases) {
            g2d.setColor(Color.BLACK);
            g2d.drawString(b.getNucleotide() + "", (float) (b.getOffsetX() * radius), (float) (b.getOffsetY() * radius));
            //g2d.drawString(b.getNucleotide() + "", (float) (b.getUnitCircleX() * radius), (float) (b.getUnitCircleY() * radius));
        }
        //loop to draw knots
        for (Knot k : knots) {
            b1 = bases.get(k.getPos1());
            arc = new Arc2D.Double();
            ucX = k.getArc().getX();
            ucY = k.getArc().getY();
            ucRadius = k.getArc().getRadius();
            ucAngSt = k.getArc().getAngSt();
            ucAngExt = k.getArc().getAngExt();
            ucClosure = k.getArc().getClosure();
            if (debug) {
                System.out.println(k.getPos1() + " " + k.getPos2() + " ucRadius " + ucRadius + " stAng: " + ucAngSt + " angExt: " + ucAngExt + " ");
            }
            if (b1.isKnot()) {
                g2d.setColor(Color.BLUE);
            }
            if (b1.isPseudoKnot()) {
                g2d.setColor(Color.RED);
            }
            Double slope, newRad, m;
            //           arc.setArcByCenter(ucX * radius, ucY * radius, ucRadius * radius, ucAngSt, ucAngExt, ucClosure);
            Point2D p1 = new Point2D.Double(bases.get(k.getPos1()).getUnitCircleX() * radius, bases.get(k.getPos1()).getUnitCircleY() * radius);
            double xMid, yMid, distance;
            Point2D p3 = new Point2D.Double(bases.get(k.getPos2()).getUnitCircleX() * radius, bases.get(k.getPos2()).getUnitCircleY() * radius);
            newRad = (p1.distance(p3)) / 2;
            System.out.println("rad" + newRad);
            xMid = ((p1.getX() - p3.getX()) / 2) + p3.getX();
            yMid = ((p1.getY() - p3.getY()) / 2) + p3.getY();
            m = (p1.getY() - p3.getY()) / (p1.getX() - p3.getX());
            if (m == 0) {
                m = 9999.0;
            } else {
                m = -1 / m;
            }
            //Point2D p2 = new Point2D.Double(2*ucX*radius,2*ucY*radius);
            //Point2D p2 = new Point2D.Double(xMid*radius,yMid*radius);
            //System.out.println(ucX + " " + ucY);
            g2d.drawString("x", (float) xMid, (float) yMid);
            Point2D p2 = new Point2D.Double(0, 0);
            arc.setArcByTangent(p1, p2, p3, (ucRadius * radius));
            g2d.draw(arc);
        }
    }
}
